package com.ug_project.views

import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import java.util.*

/**
 * Created by yeganaaa on 2/24/2017.
 */
var ViewGroup.child: Array<View>
get(){
    var views: ArrayList<View> = ArrayList()
    for (index in 0..this.childCount - 1)
    {
        views.add(this.getChildAt(index))
    }
    return views.toTypedArray()
}
set(value) {

}

var ViewGroup.childAll: Array<View>
    get(){
        var views: ArrayList<View> = ArrayList()
        fun getChildViews(viewGroup: ViewGroup)
        {
            for (item in viewGroup.child)
            {
                views.add(item)
                if (item is ViewGroup)
                    getChildViews(item)
            }
        }
        getChildViews(this)
        return views.toTypedArray()
    }
    set(value) {

    }

/**
 * بارلىق تىكىست رامكىلىرىنى تەكشۈرۈش
 * نۆۋەتتىكى بارلىق تىكىست رامكىلىرى قۇرۇق بولمىسا ئاندىن راس قايتىدۇ
 */
fun ViewGroup.checkEmptyEditTexts(emptyEditText: (editttext: EditText) -> Unit) : Boolean
{
    var temp = true
    for (item in childAll.filter { it is EditText })
    {
        if ((item as EditText).isEmpty)
        {
            temp = false
            emptyEditText(item)
            break
        }
    }
    return temp
}

fun ViewGroup.clearEdits() {
    childAll.filter { it is EditText }.forEach {
        (it as EditText).content = ""
    }
}

fun ViewGroup.checkEdits(action: () -> Unit) {
    if (this.checkEmptyEditTexts(::goEmptyEdits)) {
        action()
    }
}